<!--
 * @: 
 * @Version: 1.0
 * @Autor: ziwei
 * @Date: 2021-10-14 11:19:36
 * @LastEditors: ziwei
 * @LastEditTime: 2021-10-19 11:06:58
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=<device-width>, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    //关键在于 永远都是卖出在后边，所以只要不停的找小值和后边值的差
    let numArr = [7, 2, 5, 3, 1, 5]
    var maxProfit = function (prices)
    {
      let profit = 0;
      let min =prices[0];
      for(let i=0;i<prices.length;i++){
        if(prices[i]<min){
          min = prices[i]
        }else if(prices[i]-min>profit){
          profit = prices[i]-min
        }
      }
      return profit
    };
    console.log(maxProfit(numArr))
  </script>
</body>

</html>